{{use "jqdialog2"}}
{{use "poshytip"}}
<script>

    common_ajaxForm_success = function(options){
        return function(r){
            var opts = {
                success:null,
                message: show_message,
                done:null,
                error:null,
                field_prefix:'div_field_',
                message_type:'bootstrap'
            };
            if (typeof options === 'function'){
                opts.success = options;
            }else{
                opts = $.extend(opts, options);
            }
            if (r.success){
                opts.message(r.message||'');
                if(opts.success) opts.success.call(this, r);
                if(opts.done) opts.done.call(this, r);
            }else{
                if(opts.error) opts.error.call(this, r);
                else{
                    if(opts.message_type == 'bootstrap'){
                        $('div.control-group').removeClass('error').find('.help-block.error').remove();
                        if (r.message)
                            show_message(r.message, 'error');
                        if (r.errors){
                            $.each(r.errors, function(key, value){
                                var f, t;
                                f = '#' + opts.field_prefix + key;
                                t = $(f).addClass('error');
                                t.find('.controls').append('<p class="help-block error">'+value+'</p>');
                            });
                        }
                    }else if(opts.message_type == 'tip'){
                        var t = $(this);
                        $('body').on('dialog2.beforeClose', t, function(e){
                            t.find('input, select, textarea').poshytip('hide');
                        });
                        t.find('input, select, textarea').poshytip('hide');
                        if (r.message)
                            show_message(r.message, 'error');
                        if (r.errors){
                            $.each(r.errors, function(key, value){
                                var el = t.find('input[name='+key+'],select[name='+key+'],textarea[name='+key+']');
                                if (el.is(':hidden')){
                                    el = el.parent();
                                }
                                $(el).poshytip({
                                    className: 'tip-yellowsimple',
                                    content: value,
                                    showOn: 'none',
                                    alignTo: 'target',
                                    alignX: 'inner-left',
                                    offsetX: 0,
                                    offsetY: 5,
                                    closeButton: true
                                });
                                $(el).poshytip('show');
                                $(el).focus(function(){
                                    $(this).poshytip('hide');
                                });
                            });
                        }
                    }
                }
            }
        };
    }
    
    var dialog2 = function(url, options){
        options = options || {}
        var opts
        var default_opts = {
            autoFocus:false,
            url:url,
            width:'800px',
            autoAjax: true,
            ajaxFormOptions:{
                onSuccess: common_ajaxForm_success({
                    success:function(data){
                        dialog.dialog2('close');
                        if (opts.onSuccess) {
                            opts.onSuccess(dialog, data)
                        }
                    }
                }),
                dataType: 'json'
            }
        }
        opts = $.extend(true, {}, default_opts, options)
        var dialog = $('<div></div>').dialog2(opts);
        return dialog
    }

    $(function(){
    
        $(document).controls();
        
        $.fn.dialog2.helpers.defaults.alert = {title:'提醒', buttonLabelOk:'关闭'};
        $.fn.dialog2.helpers.defaults.prompt = {title:'提示', buttonLabelOk:'确定', buttonLabelCancel:'取消'};
        $.fn.dialog2.helpers.defaults.confirm = {title:'确认', buttonLabelYes:'是', buttonLabelNo:'否'};
    });
</script>